define([
  'jquery',
  'underscore',
  'backbone',
  'models/user/UserModel',
  'text!templates/user/addTemplate.html'
], function($, _, Backbone,UserModel, addTemplate){

  var AddView = Backbone.View.extend({
    el: $("#page"),

    render: function(){
      
      $('.menu li').removeClass('active');
      $('.menu li a[href="#/add"]').parent().addClass('active');
      this.$el.html(addTemplate);
    },
	events: {
		"click input[id='btn_create']": "doCreate"
	},
	doCreate: function(){
	   
	   var fn= $("#fn").val();
	   var ln= $("#ln").val();
	   var age=$("#age").val();
	  
	   if(localStorage.arrUser!=undefined && localStorage.arrUser.length>2){ //neu arrUser ton tai va do dai lon hon 2 (vi mang rong =[])
			var arr_user=JSON.parse(localStorage.arrUser);
			id=arr_user[0].id+1;//lay id lon nhat
	   }   else id=0; // lay id moi
	   var user = new UserModel();
		   user.set({ first_name: fn, last_name: ln, age: age, id:id});
	   var b=[]; 
	   var c=[];
	   var d=[];
	   b[0]=user;
	   if(localStorage.arrUser!=undefined) c=JSON.parse(localStorage.arrUser); // gan mang arrUser cho mang c
	   d=b.concat(c); //gop 2 mang truoc khi stringify
		localStorage.setItem("arrUser", JSON.stringify(d)); //stringify 1 mang c va gan vao arrUser
		location.href="#/user";
			
	},

  });

  return AddView;
  
});
